package com.onenine.app.view.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatDialog;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.onenine.app.R;
import com.onenine.app.utils.RoleResLoader;

/**
 * 发帖确认弹窗
 */
public class PostPublishDialog extends AppCompatDialog implements View.OnClickListener {

    private Context mContext;
    private ImageView mIvLogo;
    private Button mBtnCancel, mBtnOk;
    private View.OnClickListener mOkListener;
    private String mCoterieName;

    public PostPublishDialog(Context context, String coterieName, View.OnClickListener okListener) {
        super(context, R.style.DialogStyle);
        mContext = context;
        mCoterieName = coterieName;
        mOkListener = okListener;
        initView(context);
    }

    private void initView(Context context) {
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.layout_post_publish_dialog, null);
        setContentView(view);
        TextView tvMsg = view.findViewById(R.id.tv_msg);
        tvMsg.setText(context.getString(R.string.post_publish_confirm_hint_format, mCoterieName));
        mIvLogo = view.findViewById(R.id.iv_info_logo);
//        mIvLogo.setImageResource(RoleResLoader
//                .getRoleDrawableResourceId(RoleResLoader.DynamicResName.INFO_IMCOMPLETE_LOGO));
        mBtnCancel = view.findViewById(R.id.btn_cancel);
        mBtnCancel.setBackgroundResource(RoleResLoader
                .getRoleDrawableResourceId(RoleResLoader.DynamicResName.THEME_STROKE_WHITE_CORNER));
        mBtnCancel.setTextColor(RoleResLoader.getRoleColorResourceId(RoleResLoader.DynamicResName.THEME_COLOR));
        mBtnOk = view.findViewById(R.id.btn_confirm);
        mBtnOk.setBackgroundResource(RoleResLoader
                .getRoleDrawableResourceId(RoleResLoader.DynamicResName.THEME_GRADIENT_CORNER_BG));
        mBtnOk.setOnClickListener(this);
        mBtnCancel.setOnClickListener(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_cancel) {
            dismiss();
        } else if (v.getId() == R.id.btn_confirm) {
            dismiss();
            if (mOkListener != null) {
                mOkListener.onClick(v);
            }
        }
    }

}
